package com.itheima.controller;

import com.aliyun.oss.AliyunOSSOperator;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

//    @Autowired
//    private AliyunOSSOperator aliyunOSSOperator;
@Autowired
private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传文件：{}", file);
        if (!file.isEmpty()) {
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            // 上传文件
            String url = aliyunOSSOperator.upload(file.getBytes(), originalFilename );
            return Result.success(url);
        }
        return Result.error("上传失败");
    }

}